<?php // content="text/plain; charset=utf-8"
session_start();
require_once ("jpgraph/jpgraph.php");
require_once ("jpgraph/jpgraph_scatter.php");
include ('jpgraph/jpgraph_bar.php');
include "config.php";


function  TimeCallback ( $aVal ) {
  if(date("D",$aVal) == "Sat" || date("D",$aVal) == "Sun")
  {
    return "|".Date('d.m.',$aVal)."|";
  }  
  return  Date ( 'd.m.' , $aVal );
}

//Datenbank
$db = new mysqli($mysqlhost, $mysqluser, $mysqlpw, $mysqlname);
//Letzten Eintrag herausfinden
$lastentry = 'SELECT datentime FROM bz_werte_'.$_SESSION['id'].' ORDER BY datentime DESC';
$entries = $db->query($lastentry);
$row = $entries->fetch_assoc();
$lastday = substr($row['datentime'],0,8);
$firstday = date('Ymd',mktime(0,0,0,substr($lastday,-4,2),substr($lastday,-2)-14,substr($lastday,0,4)));
//Ist dieser Tag in der Datenbank?
$exists = 'SELECT datentime FROM bz_werte_'.$_SESSION['id'].' WHERE datentime LIKE '.$firstday.'%';
$entries = $db->query($lastentry);
/*if(mysql_num_rows($entries))
{
  //Problem
}     */


$abfrage = 'SELECT datentime,bz FROM bz_werte_'.$_SESSION['id'].' WHERE datentime > '.$firstday.'0000 ORDER BY datentime ASC';

$datax = array();
$datay = array();
$xmin = mktime(0,0,0,substr($firstday,-4,2),substr($firstday,-2),substr($firstday,0,4));
$xmax = mktime(0,0,0,substr($lastday,-4,2),substr($lastday,-2)+1,substr($lastday,0,4));
$weekend = array();

 
$entries = $db->query($abfrage);
if(!$entries)
{
  echo "Datenbankfehler!";
}

while($entryrow = $entries->fetch_assoc())
  {
    if($entryrow['bz'] != "0")
    {
      $datax[] = mktime(substr($entryrow['datentime'],-4,2),substr($entryrow['datentime'],-2,2),0,substr($entryrow['datentime'],4,2),substr($entryrow['datentime'],6,2),substr($entryrow['datentime'],0,4));
      $datay[] = $entryrow['bz'];
    }   
  }
 
$graph = new Graph(900,170);
$graph->SetFrame(false);
$graph->SetMarginColor('white');



$graph->SetScale("linlin",50,300,$xmin,$xmax);
$graph->xaxis->scale->ticks->Set(86400,43200);

 
$graph->img->SetMargin(30,20,20,55);
 
$graph->title->Set("Blutzuckerverlauf der letzten 14 Tage");
$graph->title->SetFont(FF_FONT1,FS_BOLD);

$graph->xaxis->SetLabelFormatCallback('TimeCallback');
$graph->xaxis->SetLabelAngle(90);

$graph->xgrid->Show();

$graph->SetBackgroundImage('images/graph_bg.jpg',BGIMG_FILLPLOT);
$graph->SetBackgroundImageMix(100);
$graph->img->SetAntiAliasing();


$sp1 = new ScatterPlot($datay,$datax);
$sp1->link->Show();
$sp1->link->SetWeight(1);

$sp1->mark->SetType(MARK_FILLEDCIRCLE);
$sp1->mark->SetFillColor('black');
$sp1->mark->SetWidth(0);

  
$graph->Add($sp1);
$graph->Stroke();

//
 
?>  

